﻿using System;

namespace Devme.Diagnostics
{
    internal class StopwatchScope : IDisposable
    {
        readonly IStopwatch _stopwatch;
        readonly StopwatchResultHandler<TimeSpan> _handler;

        public StopwatchScope(IStopwatchFactory stopwatchFactory, StopwatchResultHandler<TimeSpan> handler)
        {
            if (stopwatchFactory == null)
            {
                throw new ArgumentNullException("stopwatchFactory");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            _stopwatch = stopwatchFactory.Create();
            _stopwatch.Start();
            _handler = handler;
        }

        public void Dispose()
        {
            _stopwatch.Stop();
            _handler(_stopwatch.Elapsed);
        }
    }
}